找传奇、传世资源到传世资源站!

python3爬虫实例(采集淘宝商品数据)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

python爬取淘宝商品新信息,如果出现乱码,请加上如下代码:import sysreload(sys)sys.setdefaultencoding('utf8')

python3爬虫实例(采集淘宝商品数据) Python-第1张#coding=utf-8# 根据输入的关键字查询淘宝商品数据# 淘宝编码是gbkimport reimport requestsimport sysreload(sys)sys.setdefaultencoding('utf8')url = 'https://s.taobao.com/search'sp = input('请输入查询商品:')page_num = input('请输入查询页码(以逗号隔开):')ll = page_num.split(',')if len(ll) == 2 : passelif len(ll) < 2: raise ValueError('请输入查询页码信息,格式如下:1,2')payload = {'q': sp,'s': '1','ie':'utf8'} #字典传递url参数# file = open('taobao_test.txt','w',encoding='utf-8')for k in range(int(ll[0]),int(ll[1])): #100次,就是100个页的商品数据# for k in range(int(1),100): payload ['s'] = 44*k 1 #此处改变的url参数为s,s为1时第一页,s为45是第二页,89时第三页以此类推resp = requests.get(url, params = payload) print((resp.url)) #打印访问的网址 resp.encoding = 'utf-8' #设置编码 print(resp.text) title = re.findall(r'"raw_title":"([^"] )"',resp.text,re.I) #正则保存所有raw_title的内容,这个是书名,下面是价格,地址 price = re.findall(r'"view_price":"([^"] )"',resp.text,re.I) loc = re.findall(r'"item_loc":"([^"] )"',resp.text,re.I) x = len(title) #每一页商品的数量 for i in range(0,x) : #把列表的数据保存到文件中 sp_name = '' sp_price = 0 sp_loc = '' sp_name = title[i] if len(price) < x : if i < len(price): sp_price = price[i] else: sp_price = 0.00 else: sp_price = price[i] if len(loc) < x : if i < len(loc): sp_loc = loc[i] else: sp_loc = ' ' else: sp_loc = loc[i] print(str(k*44 i 1) '商品名称:' sp_name '\n' '价格:' sp_price '\n' '地址:' sp_loc '\n\n') # file.write(str(k*44 i 1) '商名:' title[i] '\n' '价格:' price[i] '\n' '地址:' loc[i] '\n\n')# file.close()

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复